import { pb } from '../pb';
import { timeTween } from '../animate/timeTween';
import Card from './card';
import Display from './display';
import Images from './images';
import ImagesService from './images-service';
import Web from './web';
import { fadeIn } from '../animate/fadeIn';
export default class Page extends Card {
  constructor(
    public data: pb.IPage,
    private is: ImagesService,
    private display: Display,
  ) {
    super(display.pages, data.time, display.ticker);
  }
  async reset(width: number, height: number) {
    this.container.removeChild();
    if (this.data.backgroundUrl) {
      const s = await this.is.getSprite(this.data.backgroundUrl);
      s.x = 0;
      s.y = 0;
      s.width = width;
      s.height = height;
      this.container.addChild(s);
    }
    for (const b of this.data.blocks) {
      switch (b.type) {
        case pb.Block.Type.WEB:
          const web = new Web(b.web)
          web.container.x = b.x;
          web.container.y = b.y;
          web.container.width = b.web.width;
          web.container.height = b.web.height;
          this.container.addChild(web.container);
          this.display.webs.set(b.web.id, web)
          await web.reset();
          // 淡入
          // fadeIn(this.display.ticker, web.container)
          break;
        // 图像
        default:
          const images = new Images(b.images, this.is, this.display.ticker)
          images.container.x = b.x;
          images.container.y = b.y;
          images.container.width = b.images.width;
          images.container.height = b.images.height;
          this.container.addChild(images.container);
          this.display.images.set(b.images.id, images)
          await images.reset();
      }
    }
  }
}
